import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog
from PyQt5.QtGui import QPixmap
from qt_03ImageView_ui import Ui_MainWindow


class MyWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.pushButton.clicked.connect(self.open_image_dialog)

    def open_image_dialog(self):
        # 打开图片文件对话框
        file_path, _ = QFileDialog.getOpenFileName(self, "选择图片", "", "Images (*.png *.jpg *.jpeg *.bmp)")
        if file_path:
            # 将图片路径显示到文本框
            self.lineEdit.setText(file_path)
            # 在标签中显示图片
            pixmap = QPixmap(file_path)
            self.label.setPixmap(pixmap)
            self.label.setScaledContents(True)  # 图片自适应标签大小


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())